def openapi(type):
    def wrapper(func):
        def inner_wrapper(*args, **kwargs):
            if type == "field":
                return f"""{get_field_annotation(f"""description = "{args[0].comment}"{', required = true' if args[1] and args[0].required() else ''}""")}
    {func(*args, **kwargs)}"""
            elif type == "api":
                function_name = func.__name__
                if function_name == "code_save":
                    api_description = "添加"
                    response_type = "Boolean.class"
                elif function_name == "code_remove_list":
                    api_description = "删除"
                    response_type = "Boolean.class"
                elif function_name == "code_update":
                    api_description = "修改"
                    response_type = "Boolean.class"
                elif function_name == "code_nullify_or_revert":
                    api_description = "作废或还原"
                    response_type = "Boolean.class"
                elif function_name == "code_swap":
                    api_description = "移动"
                    response_type = "Boolean.class"
                elif function_name == "code_get":
                    api_description = "按主键查询单体"
                    response_type = f"""{args[0].entity.name}VO.class"""
                elif function_name == "code_get_by_condition":
                    api_description = "按条件查询单体"
                    response_type = f"""{args[0].entity.name}VO.class"""
                elif function_name == "code_page":
                    api_description = "分页"
                    response_type = f"""{args[0].entity.name}VO.class"""
                elif function_name == "code_list":
                    api_description = "按条件查询列表"
                    response_type = f"""{args[0].entity.name}VO.class"""
                elif function_name == "code_next_sort":
                    api_description = "查询下一个排序号"
                    response_type = "Integer.class"
                elif function_name == "code_exists":
                    api_description = "按条件查询单体是否存在"
                    response_type = "Boolean.class"
                else:
                    assert 1 == 0, "Open API注解生成失败"
                return f"""
    {get_api_item_annotation(f"""value = "{api_description}", response = {response_type}""")}{func(*args, **kwargs)}"""
            return func(*args, **kwargs)

        return inner_wrapper

    return wrapper


def get_field_annotation(slot):
    """
    Open API Model字段注解
    """

    return f"""@Schema({slot})"""


def get_api_item_annotation(slot):
    """
    Open API Controller方法注解
    """

    return f"""@Operation({slot})"""


def get_api_annotation(comment):
    """
    Open API Controler类注解
    """
    return f"""@Tag(tags = "{comment}接口")"""


def get_api_model_annotation(entity_name, comment):
    """
    Open API Model注解
    """
    return f"""@Schema(description = "{comment}")"""


def get_includes(type):
    """
    Open API 依赖
    :type field|api|model
    """
    include_list = list[str]()
    if type == "api":
        include_list.append("import io.swagger.v3.oas.annotations.tags.Tag;")
        include_list.append("import io.swagger.v3.oas.annotations.Operation;")
    elif type == "model":
        include_list.append("import io.swagger.v3.oas.annotations.media.Schema;")
    return include_list
